package com.chinasoft.controller;

import com.chinasoft.common.Result;
import com.chinasoft.domain.Student;
import com.chinasoft.query.StudentQuery;
import com.chinasoft.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller//表示它是一个控制器
@CrossOrigin//跨域
@RestController//相当于 @Controller+@ResponseBody
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 查询所有
     * @return
     */
    //@ResponseBody//让返回的数据变为json格式
    @GetMapping("/student")
    public List<Student> loadAll(){
        List<Student> students = studentService.loadAll();
        return students;
    }

    /**
     * 根据姓名查询
     * @param name
     * @return
     */
    @GetMapping("/student/{name}")
    public Student loadByName(@PathVariable("name") String name){//@PathVariable 路径参数注解
        return studentService.loadByName(name);
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/student/{id}")//http://localhost:8082/student/3
    public Result deleteById(@PathVariable("id") Integer id){
       return studentService.deleteById(id)?Result.suc():Result.fail();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/student/batchDelete")
    public Result batchDelete(@RequestBody List<Integer> ids){
        return studentService.batchDelete(ids)?Result.suc():Result.fail();
    }

    /**
     * 新增学生
     * @param student
     */
    @PostMapping("/student")
    public Result add(@RequestBody Student student){//@RequestBody 将JSON格式变为指定的对象格式
         return studentService.addStudent(student)?Result.suc():Result.fail();
    }

    /**
     *  修改学生
     * @param student
     */
    @PutMapping("/student")
    public Result update(@RequestBody Student student){
        return studentService.updateStudent(student)?Result.suc():Result.fail();
    }
    /**
     * 分页查询
     */
    @PostMapping("/student/pageQuery")
    public Result pageQuery(@RequestBody StudentQuery studentQuery){
        return  studentService.pageQuery(studentQuery);
    }
}
